provider "openstack" {
  user_name   = "{{ openstack_username }}"
  tenant_id   = "{{ openstack_projectId }}"
  password    = "{{ openstack_password }}"
  auth_url    = "{{ openstack_identity }}"
  region      = "{{ region }}"
  user_domain_name = "{{ openstack_domain_name }}"
}



{% for host in hosts %}
    {% if host.zone.openstack_ip_type=='private' %}
        resource "openstack_networking_port_v2" "{{host.short_name}}" {
          name               = "{{host.short_name}}"
          network_id = "{{host.zone.openstack_network}}"
          admin_state_up     = "true"
          fixed_ip {
            subnet_id  = "{{host.zone.openstack_subnet}}"
            ip_address = "{{host.ip}}"
          }
        }

        resource "openstack_compute_instance_v2" "{{host.short_name}}" {
          name            = "{{host.name}}"
          image_name        = "{{image_name}}"
          {% if host.role=='worker' %}
           flavor_name          = "{{worker_model}}"
          {% endif %}
          {% if host.role=='master' %}
           flavor_name          = "{{master_model}}"
          {% endif %}
          admin_pass = "KubeOperator@2019"
          user_data = "#!/bin/sh \necho 'root:KubeOperator@2019' | chpasswd"
          security_groups = ["{{host.zone.openstack_security_group}}"]
          network {
            port = "${openstack_networking_port_v2.{{host.short_name}}.id}"
          }
        }
    {% endif %}

    {% if host.zone.openstack_ip_type=='floating' %}
        resource "openstack_networking_port_v2" "{{host.short_name}}" {
          name           = "{{host.short_name}}"
          admin_state_up = "true"
          network_id = "{{host.zone.openstack_network}}"
        }

        resource "openstack_compute_instance_v2" "{{host.short_name}}" {
          name            = "{{host.name}}"
          image_name        = "{{image_name}}"
          {% if host.role=='worker' %}
           flavor_name          = "{{worker_model}}"
          {% endif %}
          {% if host.role=='master' %}
           flavor_name          = "{{master_model}}"
          {% endif %}
          admin_pass = "KubeOperator@2019"
          user_data = "#!/bin/sh \necho 'root:KubeOperator@2019' | chpasswd"
          security_groups = ["{{host.zone.openstack_security_group}}"]
          network {
            port = "${openstack_networking_port_v2.{{host.short_name}}.id}"
          }
        }

        resource "openstack_networking_floatingip_v2" "{{host.short_name}}" {
          pool = "{{host.zone.openstack_floating_network}}"
          address = "{{host.ip}}"
        }

        resource "openstack_compute_floatingip_associate_v2" "{{host.short_name}}" {
          floating_ip = "${openstack_networking_floatingip_v2.{{host.short_name}}.address}"
          instance_id = "${openstack_compute_instance_v2.{{host.short_name}}.id}"
          fixed_ip    = "${openstack_compute_instance_v2.{{host.short_name}}.network.0.fixed_ip_v4}"
        }
    {% endif %}

{% endfor %}